<?php

/*
 * Copyright(c)2016 All rights reserved.
 * @Licenced  http://www.w3.org
 * @Author  liutian<1538731090@qq.com> liutian_jiayi
 * @Create on 2016-6-21 11:20:46
 * @Version 1.0
 */
namespace Admin\Controller;
class LoginController extends \Think\Controller{
	//显示登陆页的方法
	public function index() {
//		echo "这是登陆页面";
		$this->display();
	}
	
	//生成验证码的方法
	public function verifyCode() {
		$config = array(
			"useImgBg" => false,		//去除背景
			"fontSize" => 14,			//设置字体大小
			"length" => 4,				//验证码个数
			"imageW" => 100,			//图片宽度
			"imageH" => 26,				//图片高度
			"useCurve" => false,		//去除干扰线
			"useNoise" => false,		//去除干扰点
		);
		$verify = new \Think\Verify($config);
		$verify->entry();
	}
	
	//验证验证码是否正确
	public function chkVerify($code) {
		$verify = new \Think\Verify();
		return $verify->check($code);
	}
	
	//登陆验证功能入口
	public function logTodo() {
		/**
		 * 1、验证验证码是否正确
		 * 2、验证有效性（是否填写、格式是否正确等等）
		 * 3、验证真实性（账号是否存在 密码是否错误）
		 */
		if (!$this->chkVerify(I("post.code")))	{
			$this->error("验证码错误！");
		}
		//使用自动验证
		$userModel = D("User");
		if (false == $result = $userModel->create()) {
			exit($userModel->getError());
		}
		//验证真实性
		$userRec = $userModel->where(array("userName" => I("post.userName")))->find();
		if (empty($userRec)) {
			$this->error("账号不存在");
		} else if ($userRec['password'] != md5(I("post.password"))) {
			$this->error("密码不正确");
		} else {//账号和密码都对
			session("userData", $userRec);
			$this->success("登陆成功", U("Index/index"),2);
		}
	}
	
	//用户退出
	public function logout() {
		//销毁session
		session("userData", null);
		//跳转到首页
		$this->success("退出成功，正在跳转首页", U("Index/index"));
	}
}
